require "proto/instance/message/ReqInstanceBossMessage"
require "proto/instance/message/ReqInstanceSelectBuffMessage"
require "proto/instance/message/ReqInstanceSelectLayerMessage"
require "proto/instance/bean/InstanceFormation"
require "proto/instance/message/ReqInstanceGetRanksMessage"
require "proto/instance/message/ReqInstanceRushMessage"
require "proto/instance/message/ReqInstanceInfoMessage"

mythicDungeonsHandler = mythicDungeonsHandler or {};
G.serverData = G.serverData or {};

--服务器返回大秘境信息
function mythicDungeonsHandler.ResInstanceInfoHandler(msg)
    --log("mythicDungeonsHandler.receiveInstanceInfo");
    --dataTable.PrintTable(msg);
    if msg.state == 0 then
        logicMgr.MythicDungeonsManager.setInfo(msg);
        FireEvent(Config.EventType.Fresh_MythicDungonsInfo);
    end
end

--服务器返回boss挑战结果
function mythicDungeonsHandler.ResInstanceBossResultHandler(msg)

    if msg.state ~= 0 then
        logError("进入战斗错误-战斗返回失败 code:"..msg.state)
        FireEvent(Config.EventType.LoadingCancelByFightError,"战斗返回失败")
        return
    end
    BattlePlaybackManager.ClearMultiFightData()
    local count = 0
    local time = 0
    local winTeam = 0;--己方获胜的队伍
    local enemyWinTeam = 0;--敌方获胜的队伍
    for k,v in pairs(msg.battleResult) do
        --该场战斗的队伍id
        local teamId = k;--我方该场战斗的队伍id

        local enemyTeamId = k;--敌方该场战斗的队伍id

        if v.win > 0 then
            if winTeam == 0 then
            winTeam = k
            end
            count = count + 1
            time = time + v.time
        else
            if enemyWinTeam == 0 then
            enemyWinTeam = k
            end
            time = time + dataTable.getGlobalInt(135)
        end
        if tonumber(k) == 3 then
            --第三场战斗队伍id是胜利的队
            teamId = winTeam;
            enemyTeamId =  enemyWinTeam;
        else
            --初始化己方队伍展示
            MythicDungeonsManager.SetBattleFormations(v.record.attacker.members,1,teamId)
            --初始化敌方队伍展示
            MythicDungeonsManager.SetBattleFormations(v.record.defender.members,2,teamId)
        end
        BattlePlaybackManager.AddMultiFightData(v.fightId,v.win,EnumConst.FightTypeConst.INSTANCE,v.record,msg.items,teamId,enemyTeamId)
    end
    print("战斗耗时："..time)
    if count >= 2 then
        logicMgr.MythicDungeonsManager.AddLayer()
    end
    logicMgr.MythicDungeonsManager.setBattleRes(msg)
    MythicDungeonsManager.setBattleTime(time)
    BattlePlaybackManager.GotoNextFight(false)

    --永劫之境
    local _mythicDic = {}
    local _roundInfo1 = MythicDungeonsManager.getRoundInfos()[1]
    if _roundInfo1 then
        _mythicDic.current_layer = tonumber(_roundInfo1.layer)
        _mythicDic.max_layer = tonumber(_roundInfo1.maxLayer)
        _mythicDic.buff_id = tonumber(_roundInfo1.buffId) 
    end
    _mythicDic.battle_count = #msg.battleResult
    _mythicDic.running_time = time
    _mythicDic.is_pass = count >= 2 and 1 or 0
    for _, value in pairs(msg.battleResult) do
        _mythicDic["fight_id_"..value.round] = tostring(value.fightId)
        _mythicDic["is_win_"..value.round] = tonumber(value.win)
        _mythicDic["time_"..value.round] = tonumber(value.time)
    end
    if _mythicDic.battle_count == 2 then
        _mythicDic["fight_id_3"] = -1
        _mythicDic["is_win_3"] = -1
        _mythicDic["time_3"] = -1
    end

    local _allCount = G.dataTable["t_global"]["80"].f_int_value + _mythicDic.max_layer
    _mythicDic.max_mythic_dungeons_level_id = G.dataTable["t_global"]["186"].f_int_value + _allCount
    TAManager.TrackByDictionary("mythic_battle_result",_mythicDic)
end

--服务器返回buff选择
function mythicDungeonsHandler.receiveSelectBuff(msg)
    --log("mythicDungeonsHandler.receiveSelectBuff");
    --dataTable.PrintTable(msg);
    if msg.state ==0 then
        logicMgr.MythicDungeonsManager.setBuff(msg);
        FireEvent(Config.EventType.Fresh_MythicSelectBuff);
    end
end

--服务器返回层数选择
function mythicDungeonsHandler.receiveSelectLayer(msg)
    --log("mythicDungeonsHandler.receiveSelectLayer");
    --dataTable.PrintTable(msg);
    if msg.state == 0 then
        logicMgr.MythicDungeonsManager.setLayer(msg);
        FireEvent(Config.EventType.Fresh_MythicSelectLayer);
    end
end

--发送挑战boss请求
function mythicDungeonsHandler.sendBoss(layerId,formations)
    --mythicDungeonsHandler.sendDungeon(layerId,true,{});
    local req = ReqInstanceBossMessage.New();
    req.layerId = layerId;
    req.version = Config.ProtoVersion;
    req.formations = {};
    for i = 1, #formations do
        local data = InstanceFormation.New();
        data.round = i;
        data.formation = formations[i]
        table.insert(req.formations,data);
    end
    sendReqMessage(req);
end

--发送选择buff请求
function mythicDungeonsHandler.sendSelectBuff(layerId,buffId)
    local req = ReqInstanceSelectBuffMessage.New();
    req.layerId = layerId;
    req.buffId = buffId;
    sendReqMessage(req);
end

--发送选择层数请求
function mythicDungeonsHandler.sendSelectLayer(layerId)
    local req = ReqInstanceSelectLayerMessage.New();
    req.layerId = layerId;
    sendReqMessage(req);
end
--排行榜
function mythicDungeonsHandler.ReqInstanceGetRanks()
    local req = ReqInstanceGetRanksMessage.New();
    req.rankVersion = -1
    sendReqMessage(req);
end
--- @param msg ResInstanceGetRanksMessage 排行榜返回
function mythicDungeonsHandler.ResInstanceGetRanksHandler(msg)
    --if MythicDungeonsManager.rankVersion == msg.rankVersion then
        --FireEvent(Config.EventType.Fresh_MythicRanks)
    --    return
    --end
    logicMgr.MythicDungeonsManager.rankVersion = msg.rankVersion
    logicMgr.MythicDungeonsManager.SetRanks(msg.ranks)
    logicMgr.MythicDungeonsManager.SetMyRank(msg.myRank,msg.myScore)
    FireEvent(Config.EventType.Fresh_MythicRanks)
end
--扫荡
function mythicDungeonsHandler.ReqRush(time)
    local req = ReqInstanceRushMessage.New()
    req.times = time
    sendReqMessage(req)
end
function mythicDungeonsHandler.ResRush(msg)
    logicMgr.MythicDungeonsManager.RushWards(msg)
    FireEvent(Config.EventType.RushWwards)
    FireEvent(Config.EventType.Fresg_RushItem)
end
--获取永劫之境数据
function mythicDungeonsHandler.ReqInstanceInfo()
    local req = ReqInstanceInfoMessage.New();
    sendReqMessage(req);
end